<?php
class lib_company_zone extends lib_base{
	public $table_name = "qc_company_zone";
    public $pk = "id";



    public function add($data)
    {
        $data['status'] = 1;
        $data['add_time'] = date('Y-m-d H:i:s');
        return $this->create($data);
    }


	// public function get_pname($id)
    // {
    //     $info = $this->findById($id);
    //     return $this->create($data);
    // }

    // public function get_zone_ids($id)
    // {
    //     $tempTreeArr = [];
    //     $info = $this->findById($id);
    //     if($info){
    //         $tempTreeArr[] = $info['id'];
    //         if($info['parent_id']){
    //             $tempTreeArr = array_merge($tempTreeArr,$this->get_zone_ids($info['parent_id']));
    //         }
    //     }
    //     return $tempTreeArr;
    // }

    //二维码
	public function qrcode($url,$info,$company,$dir2=''){
		header("Content-Type: text/html;charset=utf-8");
		// $id = arg('id');
		// $this->info = $this->findById($id);
		// $this->zone = $this->zoneObj->findById($this->info['zone_id']);

		// require(APP_DIR.'/protected/include/lib/phpqrcode.php');
		$value = $url;//二维码内容
		// $value = "/index/main/qrcode?device_id=".$id;//二维码内容

		$errorCorrectionLevel = 'L';//容错级别
		$matrixPointSize = 6;//生成图片大小
		$cnname = $company['name'].'<br />'.$info['name'];
		// $name2 = iconv('UTF-8', 'GB18030', $name);
        // $name = $this->zone['id'].'_'.$this->info['id'];
        $name = $company['id'].'_'.$info['id'];

		$dir = "./upload/zone/{$company['id']}/";
        if($dir2){
            $dir .= "{$dir2}/";
        }
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }

		//生成二维码图片
		$file = $dir.$name.'.png';
		$code = $dir.$name.'_code.png';
		QRcode::png($value, $file, $errorCorrectionLevel, $matrixPointSize, 6);

		if($company['logo']){
			$logo = './upload/'.$company['logo'];//准备好的logo图片
		}else{
			$logo = './upload/logo.png';
		}
		$QR = $file;//已经生成的原始二维码图

		if ($logo !== FALSE) {
		 $QR = imagecreatefromstring(file_get_contents($QR));
		 $logo = imagecreatefromstring(file_get_contents($logo));
		 $QR_width = imagesx($QR);//二维码图片宽度
		 $QR_height = imagesy($QR);//二维码图片高度
		 $logo_width = imagesx($logo);//logo图片宽度
		 $logo_height = imagesy($logo);//logo图片高度
		 $logo_qr_width = $QR_width / 5;
		 $scale = $logo_width/$logo_qr_width;
		 $logo_qr_height = $logo_height/$scale;
		 $from_width = ($QR_width - $logo_qr_width) / 2;
		 // $logo_qr_height = $logo_qr_height + 110;
		 // dump($logo_qr_width);exit;
		 //重新组合图片并调整大小
		 imagecopyresampled($QR, $logo, $from_width, $from_height, 0, 0, $logo_qr_width,
		 $logo_qr_height, $logo_width, $logo_height);
		}


		$text = $company['name']."--".$info['name']."({$info['id']})";
		$this->mark_photo($file,$text,$file);


		$file = str_replace('./', '', $file);

		//在图片中添加文字拼接图片
		//输出图片
		// Header("Content-type: image/png");
		// ImagePng($QR,$code);

		// echo $cnname.'<br />';
		// echo '<img src="/'.$file.'"  alt="" />';
        return $file;

    }

    public function mark_photo($background,$text,$filename){

		$image = imagecreatefrompng($background);
		$font = './static/app/fonts/msyh.TTF';
		$color = imagecolorallocate($image,0,0,0);
		imagettftext($image, 10, 0, 60, 25, $color, $font, $text);
		// header("Content-Type:image/png");
		ImagePng($image, $filename);
		imagedestroy($image);
	}


}